/*
* @file    :drv_timer.c
* @module  :TIMER
* @author  :LingFengLu
* @date    :2023-06-06
*/

#include "drv_timer.h"
#include "log.h"
#include "lib_string.h"
//Constant definition    宏定义
#define TIMEOUT_PERIOD_MIX    1//MS
/* High Speed start up time */
#define HS_STARTUP_TIME 320 // 780 us/* No Calibration */
#define INITIAL_CALIBRATION         FALSE
#define CALIBRATION_INTERVAL        0

//Type definition    类型定义
typedef struct drv_timer
{
	VTIMER_HandleType  HandleTimer ; 
	uint32 u32_Period; //周期
	uint8 b_Repeat; //单次还是一次
	bool timeouflag; //单次还是一次
}drv_timer;




//Global variable definition    全局变量定义



//Private function declaration   私有函数申明
static void MX_TIMx_Init(void) ;
//Private variable definition    私有变量定义

static drv_timer m_t_Timers[DRV_TIMER_COUNT] = {0};
volatile drv_systimer systimer[DRV_TIMER_COUNT] = {0};

//Public function definition   公共函数定义
void DrvTimer_Init(void)
{
	HAL_VTIMER_InitType VTIMER_InitStruct = {HS_STARTUP_TIME, INITIAL_CALIBRATION, CALIBRATION_INTERVAL};
	HAL_VTIMER_Init(&VTIMER_InitStruct);
}

//开的流程：1.设置周期 2.设置是否单次 3. 设置回调  4. 设置开始 5. 超时后，设置结束标志
//关的流程：1. 设置关闭 2.清空结构体
uint DrvTimer_SetConfig
(
uint ui_DeviceId,
uint ui_Parameter,
uint8 *u8p_Value,
uint ui_Length
)
{
	if(ui_DeviceId >= DRV_TIMER_COUNT)
		return FUNCTION_FAIL;
//	if(ui_DeviceId == DRV_TIMER_ID0)
//	{
//		switch (ui_Parameter)
//		{
//		    case DRV_TIMER_PARAM_SWITCH:
//			 systimer[ui_DeviceId].u8_swtich = *u8p_Value ;
//			break; 
//		    case DRV_TIMER_PARAM_PERIOD:
//			 systimer[ui_DeviceId].u32_Period  = *u8p_Value ;
//			 systimer[ui_DeviceId].u32_Period_count =  systimer[ui_DeviceId].u32_Period ;
//			break; 
//		    case DRV_TIMER_PARAM_CALLBACK:
//			systimer[ui_DeviceId].Callback = (VTIMER_CallbackType)u8p_Value ;
//			break; 
//		    case DRV_TIMER_PARAM_REPEAT:
//			systimer[ui_DeviceId].b_Repeat   = *u8p_Value ;
//			break; 
//		    case DRV_TIMER_PARAM_TIMEOUTFLAG:
//			break;   
//		    default:
//			break;
//		}
//	}
//	else
	{
		switch (ui_Parameter)
		{
		    case DRV_TIMER_PARAM_SWITCH:
			if(*u8p_Value == 1)
			{
				HAL_VTIMER_StartTimerMs(&m_t_Timers[ui_DeviceId].HandleTimer, m_t_Timers[ui_DeviceId].u32_Period);
			}
			else
			{
				HAL_VTIMER_StopTimer(&m_t_Timers[ui_DeviceId].HandleTimer) ;
				return FUNCTION_FAIL;
			}
			
			break;
		    case DRV_TIMER_PARAM_PERIOD:
			if(ui_Length < TIMEOUT_PERIOD_MIX)
				return FUNCTION_FAIL;
			m_t_Timers[ui_DeviceId].u32_Period = (*(uint32 *)u8p_Value);
			break;
		    case DRV_TIMER_PARAM_CALLBACK:
			m_t_Timers[ui_DeviceId].HandleTimer.callback = (VTIMER_CallbackType)u8p_Value ;
			break;
		    case DRV_TIMER_PARAM_REPEAT:
			if(*u8p_Value  == 0)
				m_t_Timers[ui_DeviceId].b_Repeat = false;
			else
				m_t_Timers[ui_DeviceId].b_Repeat = true;
			break; 
		    case DRV_TIMER_PARAM_TIMEOUTFLAG:
			m_t_Timers[ui_DeviceId].timeouflag = (*(uint32 *)u8p_Value);
			break;
		    default:
			break;
		}
	}
	return FUNCTION_OK;
}

void DrvTimer_Start(uint ui_DeviceId ,uint32 timeout ,uint8 Repeat  ,VTIMER_CallbackType Callback)
{
	uint ret ;
//	if(ui_DeviceId == DRV_TIMER_ID0)
//	{
//		systimer[ui_DeviceId].u8_swtich  = 1 ;
//		systimer[ui_DeviceId].b_Repeat = Repeat ;
//		systimer[ui_DeviceId].u32_Period = timeout ;
//		systimer[ui_DeviceId].u32_Period_count =  systimer[ui_DeviceId].u32_Period ;
//		systimer[ui_DeviceId].Callback =Callback ; 
//	}
//	else
	{
		//设置周期
		m_t_Timers[ui_DeviceId].u32_Period = timeout ;
		//设置是否单次
		m_t_Timers[ui_DeviceId].b_Repeat = Repeat ;
		//设置回调
		m_t_Timers[ui_DeviceId].HandleTimer.callback = Callback ;
		//开始
		ret = HAL_VTIMER_StartTimerMs(&m_t_Timers[ui_DeviceId].HandleTimer, m_t_Timers[ui_DeviceId].u32_Period);
		if(ret != BLE_STATUS_SUCCESS)
		{
			LOG_INFO("HAL_VTIMER_StartTimerMs()failed: 0x%02x\r\n", ret);
		}
	}
}

void DrvTimer_Finsh(uint ui_DeviceId)
{
	//关闭
	HAL_VTIMER_StopTimer(&m_t_Timers[ui_DeviceId].HandleTimer) ;
	//清空结构体
	Lib_Memset( (uint8 *)&m_t_Timers[ui_DeviceId], 0 ,sizeof(drv_timer) ) ;
}

void DrvTimer_process(void)
{
	uint8 u8_Value ;
	for(uint i = 0 ; i < DRV_TIMER_COUNT ;i++)
	{
		if(m_t_Timers[i].b_Repeat)
		{
			if(m_t_Timers[i].timeouflag)
			{
				m_t_Timers[i].timeouflag = 0 ;
				u8_Value = 1 ;
				DrvTimer_SetConfig(i ,DRV_TIMER_PARAM_SWITCH ,&u8_Value,m_t_Timers[i].u32_Period) ;
			}
		}
		else
		{
			if(m_t_Timers[i].timeouflag)
			{
				m_t_Timers[i].timeouflag = 0 ;
				//设置关闭
				u8_Value = 0 ;
				DrvTimer_SetConfig(i ,DRV_TIMER_PARAM_SWITCH ,&u8_Value,m_t_Timers[i].u32_Period) ;
				//清空结构体
				Lib_Memset( (uint8 *)&m_t_Timers[i], 0 ,sizeof(drv_timer) ) ;
			}
		}
	}
	
}

//Private function definition   私有函数定义
